<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="所属公司" prop="companyId">
        <el-select v-model="queryParams.companyId" placeholder="请选择所属公司" clearable size="small" @change="handleQuery">
          <el-option v-for="item in companys" :key="item.id" :label="item.text" :value="item.id"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:figure:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:figure:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:figure:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:figure:export']"
        >导出</el-button>
      </el-col> -->
    </el-row>

    <el-table v-loading="loading" :data="figureList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
       <el-table-column fixed label="序号" align="center">
        <template slot-scope="scope">
          <span>{{(queryParams.pageNum-1)*queryParams.pageSize+(scope.$index + 1)}} </span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="主键id" align="center" prop="id" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="链接" align="center" prop="url" /> -->
<!--      <el-table-column label="图片" align="center" prop="icon">-->
<!--        <template slot-scope="scope">-->
<!--          <img :src="scope.row.icon" style="max-width: 120px" />-->
<!--        </template>-->
<!--      </el-table-column>-->

      <el-table-column label="图片" align="center" prop="icon" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.icon" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <!-- <el-table-column label="类型" align="center" prop="type" />
      <el-table-column label="参数" align="center" prop="param" /> -->
      <!-- <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="排序" align="center" prop="priority" />
      <el-table-column label="描述" align="center" prop="description" /> -->
      <el-table-column label="所属公司" align="center" prop="companyName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:figure:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:figure:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="链接" prop="url">
          <el-input v-model="form.url" placeholder="请输入链接" />
        </el-form-item> -->
        <!-- <el-form-item label="文件" prop="icon">
          <el-input v-model="form.icon" placeholder="请输入图标" />
        </el-form-item> -->
        <el-form-item label="所属公司" prop="companyId">
          <el-select v-model="form.companyId" placeholder="请选择所属公司" clearable size="small" style="width: 100%;">
            <el-option v-for="item in companys" :key="item.id" :label="item.text" :value="Number(item.id)"></el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="图片" prop="icon">-->
<!--        <div class="updateimg">-->
<!--          <el-upload-->
<!--            :action="MixinUploadAllApi"-->
<!--            :on-success="uploadSuccess"-->
<!--            :file-list="fileListqiye"-->
<!--            :multiple="false"-->
<!--            :limit="1"-->
<!--            :before-upload="beforeImgUpload"-->
<!--          >-->
<!--            <el-button size="small" type="primary">点击上传</el-button>-->
<!--            <div slot="tip" class="el-upload__tip" style="width: 300px">-->
<!--              建议上传jpg/png文件，尺寸（宽*高）750*327-->
<!--            </div>-->
<!--          </el-upload>-->
<!--          <img class="iconImg" :src="icon" />-->
<!--        </div>-->
<!--      </el-form-item>-->

        <el-form-item label="图片" prop="icon">
          <image-upload v-model="form.icon"/>
        </el-form-item>


        <!-- <el-form-item label="类型">
          <el-select v-model="form.type" placeholder="请选择类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="参数" prop="param">
          <el-input v-model="form.param" placeholder="请输入参数" />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序" prop="priority">
          <el-input v-model="form.priority" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入描述" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFigure, getFigure, delFigure, addFigure, updateFigure, exportFigure } from "@/api/shufflingFigure/figure";
import { getCompanys } from '@/api/system/dept';

export default {
  name: "Figure",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      fileListqiye: [],
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      figureList: [],

      icon: '',
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        url: undefined,
        icon: undefined,
        type: undefined,
        param: undefined,
        status: undefined,
        priority: undefined,
        description: undefined
      },
      MixinUploadAllApi: process.env.VUE_APP_BASE_API + "/file/upload/single",
      // 表单参数
      form: {},
       // 公司信息
      companys: [],
      // 表单校验
      rules: {
         icon: [
          { required: true, message: "轮播图不能为空", trigger: "blur" }
        ],
        companyId: [
            { required: true, message: '请选择所属公司', trigger: 'change' }
        ],
      }
    };
  },
  created() {
    this.getList();
     this.getCompanyList();
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listFigure(this.queryParams).then(response => {
        this.figureList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        url: undefined,
        icon: undefined,
        type: undefined,
        param: undefined,
        status: "0",
        priority: undefined,
        description: undefined
      };
      this.resetForm("form");
    },
         // 主体
    uploadSuccess(res) {
      this.icon = res.data
      this.form.icon = res.data;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加轮播图片";
      this.fileListqiye = [];
      this.form.icon = '';
    },
        /** 图片上传之前的校验  */
    beforeImgUpload(file) {
      const isLt1M = file.size / 5120 / 5120 < 1;
      if (!isLt1M) {
        this.msgWarning("上传图片大小不能超过 5MB!");
      }
      var FileExt = file.name.replace(/.+\./, "");
      if (["jpg", "jpeg", "png"].indexOf(FileExt.toLowerCase()) === -1) {
        this.msgWarning("请上传后缀名为jpg/jpeg/png的图片附件！");
        return false;
      }
      return isLt1M;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getFigure(id).then(response => {
        this.form = response.data;
        this.icon = response.data.icon
        this.open = true;
        this.title = "修改轮播图片";
        this.form.icon = response.data.icon;
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      // this.form.icon = this.icon;
      this.$refs["form"].validate(valid => {
        if (valid) {
            // console.log(this.form)
            // return ;
          if (this.form.id != undefined) {
            updateFigure(this.form).then(response => {
              if (response.code == 0) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addFigure(this.form).then(response => {
              if (response.code == 0) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delFigure(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有【请填写功能名称】数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportFigure(queryParams);
        }).then(response => {
          this.download(response.data);
        }).catch(function() {});
    },
    /** 获取公司信息 */
    getCompanyList() {
      getCompanys().then(response => {
        this.companys = response.data;
      });
    },
  }
};
</script>
<style>
 .news-image {
    width: 50px;
    height: 50px;
  }
  .updateimg {
  width: 20%;
  max-width: 500px;
  min-width: 200px;
  float: left;
}
 .iconImg{
   max-width: 500px;
 }
</style>
